package getBaiduPaid;

import java.io.IOException;
import java.sql.SQLException;
import java.util.LinkedList;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import removePuncts.RemovePuncts;
import batch.FileArrayProvider;
import batch.JSave;
import batch.QueryGenerator;

public class GetBaiduPaid {
	static PaidListingFetcher plf = new PaidListingFetcher();

	public static void main (String args[]) 
			throws IOException, ClassNotFoundException, SQLException, InterruptedException {
		
		LinkedList<String> kws = new LinkedList<String>();
		JSave saver = new JSave();
		QueryGenerator qg = new QueryGenerator();
		
		//Must execute this method to connect to mysql
		Config.Init();
		
		kws=ReadListMysql(Config.batch);
		
		try{
			while (!kws.isEmpty()) {
				Document doc = plf.getSERP(qg.genQueryUrl(kws.getFirst()));
				Elements elements = plf.RankingPositioner(doc, Config.LeftOrRight);
				
				SaveResults(elements,kws.getFirst());

				kws.removeFirst();
				Thread.sleep(5000);
			}
			Config.todoListFileOb.delete();
		}catch(Exception e){
			Config.todoListFileOb.delete();
				for (String s : kws) saver.Add(Config.todoList, s);
		}
		
	}
		
	public static void ReadList() throws IOException{
		LinkedList<String> kws = new LinkedList<String>();
		
		if (Config.todoListFileOb.exists()) {
			for (String s : new FileArrayProvider().readLines(Config.todoList)) 
				kws.add(s);
		}else{
			for (String s : new FileArrayProvider().readLines(Config.keywordList))
				kws.add(s);
		}
	}
	
	public static LinkedList<String> ReadListMysql(int batch) throws IOException, ClassNotFoundException, SQLException{
		LinkedList<String> kws = new LinkedList<String>();
		KeywordQ q = new KeywordQ();
		
		if (Config.todoListFileOb.exists()) {
			for (String s : new FileArrayProvider().readLines(Config.todoList)) 
				kws.add(s);
		}else{
			for (String s : q.NextBatch(batch))
				kws.add(s);
		}
		return kws;
	}
	
	public static void SaveResults (Elements elements,String keyword) 
			throws ClassNotFoundException, SQLException, IOException {
		
		int x = 1;
		
		for (Element el : elements) {
			Config.stm.executeUpdate("INSERT INTO "+Config.Table+" VALUES " +
							"('"+Config.df.format(Config.date)+"','"+
								RemovePuncts.Remove(keyword)+"','"+
									x+"','"+
										RemovePuncts.Remove(plf.getListingTitle(el).text())+"','"+
											RemovePuncts.Remove(plf.getListingDesc(el).text())+"','"+
												plf.getListingURL(el).text()+"');");

			System.out.println(keyword+"-"+x+":"+plf.getListingTitle(el).text());
			System.out.println(keyword+"-"+x+":"+plf.getListingDesc(el).text());
			System.out.println(keyword+"-"+x+":"+plf.getListingURL(el).text());
			
			x++;
		}
		
	}
	
//	public static void Init() throws FileNotFoundException, IOException {
//		p.load(new FileInputStream(new File("./GetBaiduRanking.config")));

//		keywordList = p.getProperty("inputFileName");
//		user = p.getProperty("user");
//		password = p.getProperty("password");
//		DB = p.getProperty("db");
//		Table = p.getProperty("table_prefix")+p.getProperty("left_or_right");
		
//		todoList = p.getProperty("todoList");
		
//		df = new SimpleDateFormat("YYYY/MM/dd");
//	}
}


//for (Element el : elements) {
//	Statement stm = conn.Connect(DB, Table,user,password).createStatement();
//	stm.executeUpdate("INSERT INTO "+Table+" VALUES " +
//			"('"+df.format(date)+"','"+
//			RemovePuncts.Remove(kws.getFirst())+"','"+
//			x+"','"+
//			RemovePuncts.Remove(plf.getListingTitle(el).text())+"','"+
//			RemovePuncts.Remove(plf.getListingDesc(el).text())+"','"+
//			plf.getListingURL(el).text()+"');");
//
//	System.out.println(kws.getFirst()+"-"+x+":"+plf.getListingTitle(el).text());
//	System.out.println(kws.getFirst()+"-"+x+":"+plf.getListingDesc(el).text());
//	System.out.println(kws.getFirst()+"-"+x+":"+plf.getListingURL(el).text());
//	x++;
//}

